package DataFile;

import java.nio.ByteBuffer;
import java.util.Formatter;

import ClassData.ConstantPoolString_Info;

public class Structure_CP_StringInfo extends Structure_CP_Constant {

	ConstantPoolString_Info myStringInfo;
	
	public Structure_CP_StringInfo(ConstantPoolString_Info myStringInfo) {
		this.myStringInfo=myStringInfo;
	}

	protected int setBaseAddress(int newBaseAdress) {
		while ((newBaseAdress%2)!=0) {
			newBaseAdress++;
		}
		baseAdress=newBaseAdress;
		return(baseAdress+getMemorySize());
	}

	public int getMemorySize() {
		int size;
		
		size=BYTE_WIDTH;	// Type
		size+=SHORT_WIDTH;	// class name index
		
		return(size);	
	}	

	protected String getMemoryMapAsString() {
		String returnString;
		
		returnString=getAdressAsHexString(baseAdress);
		returnString+=" : (String info) : size - " + new Formatter().format("%3d",getMemorySize());
		returnString+=" Value : "+myStringInfo.getNameIndex()+"\n";
		
		return(returnString);
	}

	/**
	 *  writes data into memory file
	 *  
	 *  @author Tavin
	 *  
	 *  @param fileData buffer for data 
	 */
	public void writeData(ByteBuffer fileData) {
		fileData.position(baseAdress-DataFileCreator.BASE_MEMORY_ADRESS);
		fileData.put((byte)myStringInfo.getType());
		fileData.putShort(myStringInfo.getNameIndex());
	}

}
